<template>
  <a-modal
    title="新建"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="modalOk"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-row>
          <a-col :span="12">
            <a-form-item v-show="false" label="主键ID">
              <a-input v-decorator="['id', { initialValue: 0 }]" disabled />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="公司">
              <a-input v-decorator="['title', {rules: [{required: true, message: '请输入公司！'}], initialValue: model.title}]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="别名">
              <a-input v-decorator="['alias', {rules: [{required: true, message: '请输入别名！'}], initialValue: model.alias}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="税率">
              <a-input v-decorator="['tax', {rules: [{required: true, message: '请输入税率！'}], initialValue: model.tax}]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="税号">
              <a-input v-decorator="['taxNumber', {rules: [{required: true, message: '请输入税号！'}], initialValue: model.taxNumber}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="开户行">
              <a-input v-decorator="['openingBank', {rules: [{required: true, message: '请输入开户行！'}], initialValue: model.openingBank}]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="开户账号">
              <a-input v-decorator="['bankAccount', {rules: [{required: true, message: '请输入开户账号！'}], initialValue: model.bankAccount}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="电话">
              <a-input v-decorator="['telephone', {rules: [{required: true, message: '请输入电话'}], initialValue: model.telephone}]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="规格型号">
              <a-input v-decorator="['spec', {rules: [{required: true, message: '请输入规格型号'}], initialValue: model.spec}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="免税标识">
              <a-radio-group v-decorator="['yhzcbs', {rules: [{required: true, message: '请输入免税标识！'}], initialValue: (model.yhzcbs + '')}]">
                <a-radio-button value="0">免税</a-radio-button>
                <a-radio-button value="1">正常</a-radio-button>
              </a-radio-group>
              <!-- <a-input v-decorator="['yhzcbs', {rules: [{required: true, message: '请输入免税标识！'}], initialValue: model.yhzcbs}]" /> -->
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="商品类型">
              <a-input v-decorator="['spbm', {rules: [{required: true, message: '请输入商品类型！'}], initialValue: model.spbm}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item label="地址">
              <a-input v-decorator="['address', {rules: [{required: true, message: '请输入地址！'}], initialValue: model.address}]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="部门">
              <a-input style="cursor: pointer;" @click="toSelectSetting" v-decorator="['departmentName', {rules: [{required: true, message: '请选择部门！'}], initialValue: model.department && model.department.departName}]" />
              <a-input hidden style="cursor: pointer;" @click="toSelectSetting" v-decorator="['departmentId', {rules: [{required: true, message: '请选择部门！'}], initialValue: model.department && model.department.departId}]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <select-department @OnSelect="OnSelect" :visible="selectVisible" :model="this.model" @cancel="()=>{this.selectVisible=false}" @ok="modalOk" />
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { addSetting, putSetting } from '@/api/manage'
import SelectDepartment from './SelectDepartment'
const fields = ['id']

export default {
  name: 'MemberForm',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      selectVisible: false,
      form: this.$form.createForm(this)
    }
  },
  components: {
    SelectDepartment
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    // 当 model 发生改变时，为表单设置值
    const _self = this
    this.$watch('model', () => {
      this.form = this.$form.createForm(this)
      _self.$nextTick(() => {
        this.model && this.form.setFieldsValue(pick(this.model, fields))
      })
    })
  },
  methods: {
    toSelectSetting () {
      this.selectVisible = true
    },
    OnSelect (record) {
      this.selectVisible = false
      this.form.setFieldsValue({
        'departmentName': record.departName,
        'departmentId': record.id
      })
      this.model.departmentId = record.id
    },
    modalOk () {
      this.form.validateFieldsAndScroll((errors, values) => {
        if (!errors) {
          delete values.id
          if (this.model && this.model.id) {
            putSetting(this.model.id, values).then(res => {
              this.$emit('ok')
            })
          } else {
            addSetting(values).then(res => {
              this.$emit('ok')
            })
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
